#!/usr/bin/env python
'''Find images in flickr'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
__license__ = "BSD" # see LICENSE

import webbrowser

URL = "http://www.flickr.com/search/?l=comm&%s"

def findpicture(query, search_text=0):
    if isinstance(query, (tuple, list)):
        query = "+".join((item.strip() for item in query if item))

    query = "q=%s" % query
    if not search_text:
        query += "&m=tags"
    webbrowser.open(URL % query)

def main(argv=None):
    if argv is None:
        import sys
        argv = sys.argv

    from optparse import OptionParser

    parser = OptionParser("usage: %prog QUERY [QUERY2 ...]")
    parser.add_option("-t", help="search in text",
            default=0, dest="text", action="store_true")

    opts, args = parser.parse_args(argv[1:])
    if len(args) < 1:
        parser.error("wrong number of arguments") # Will exit

    findpicture(args, opts.text)

if __name__ == "__main__":
    main()
